package com.demo.apparel.mapper.uniapp;

import com.demo.apparel.entity.WxUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface WxUserMapper {
//    查询openid是否存在

    @Select("SELECT * FROM user WHERE openid = #{openid} LIMIT 1")
    WxUser selectByOpenid(@Param("openid") String openid);
    //实现插入数据
    @Insert("""
        INSERT INTO user (id, openid, username, password,avatar, phone, created_time, updated_time)
        VALUES (#{id}, #{openid}, #{username},#{password}, #{avatar}, #{phone}, NOW(), NOW())
    """)
    int insertUser(WxUser user);

    //查询用户名称
    @Select("SELECT * FROM user WHERE username = #{username} LIMIT 1")
    WxUser selectByUsername(@Param("username") String username);

    @Select("SELECT * FROM user WHERE id = #{id} LIMIT 1")
    WxUser selectById(@Param("id") String id);
    //更新用户的信息
    @Update("""
    UPDATE user
    SET username = #{username},
        avatar = #{avatar},
        phone = #{phone},
        email=#{email},
        updated_time = NOW()
    WHERE id = #{id}
""")
    int updateUser(WxUser user);

    /**
     * 单独上传头像
     */
    @Update("UPDATE user SET avatar = #{avatar}, updated_time = NOW() WHERE id = #{id}")
    int updateAvatar(@Param("id") String id, @Param("avatar") String avatar);

    // ✅ H5 登录：根据手机号和密码查询
    @Select("SELECT * FROM user WHERE phone = #{phone} AND password = #{password} LIMIT 1")
    WxUser selectByPhoneAndPassword(@Param("phone") String phone, @Param("password") String password);

    // 查询手机号是否存在（✅ H5 注册时要用到）
    @Select("SELECT * FROM user WHERE phone = #{phone} LIMIT 1")
    WxUser selectByPhone(@Param("phone") String phone);

    // 登录：支持手机号或用户名
    @Select("""
    SELECT * FROM user
    WHERE phone = #{param} OR username = #{param}
    LIMIT 1
""")
    WxUser selectByPhoneOrUsername(@Param("param") String param);
}
